<?php

class Email
{
	private $error;
	public $cookie_jar1;
	
	public function __construct()
	{}
	
	public function __destruct()
	{
		$this->clean();
	}

	public function get_contacts($account, $password)
	{
		$account = current(explode('@', $account));
		$account = strtolower(trim($account));
		$password = trim($password);
		if (empty($account) || empty($password)) {
			$this->error = 'err_01';
			return false;
		}

		$tmp_dir = PATH_SEPARATOR == ':' ? '/dev/shm/invite/' : 'c:/windows/temp/';
		$tmp_dir = $tmp_dir . substr(md5(microtime(true) . rand(0, 999999999)), 0, 2);

		$cookie_jar1 = tempnam($tmp_dir, 'cookie1');
		$this->cookie_jar1 = $cookie_jar1;

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_TIMEOUT, 3);
		curl_setopt($ch, CURLOPT_URL, 'http://passport.21cn.com/maillogin.jsp');
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, 'LoginName=' . $account . '&DomainName=21cn.com&Template=extend&UserName=' . $account . '&passwd=' . $password);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_NOBODY, false);
		$return = curl_exec($ch);
		preg_match('/Location: (.*?)\s/', $return, $m);
		if (!isset($m[1]) || strpos($m[1], 'error') !== false) {
			$this->error = 'err_01';
			curl_close($ch);
			return false;
		}

		curl_setopt($ch, CURLOPT_URL, $m[1]);
		$return = curl_exec($ch);
		preg_match('/Location: (.*?)\s/', $return, $m);
		if (!isset($m[1])) {
			$this->error = 'err_01';
			curl_close($ch);
			return false;
		}

		curl_setopt($ch, CURLOPT_URL, $m[1]);
		$return = curl_exec($ch);
		$host = parse_url($m[1], PHP_URL_HOST);
		preg_match('/window\.location\.href="(.*?)";/', $return, $m);
		if (!isset($m[1])) {
			$this->error = 'err_01';
			curl_close($ch);
			return false;
		}
		$location = $m[1];
		if (strpos($location, $host) === false) {
			$location = 'http://' . $host . $location;
		}

		curl_setopt($ch, CURLOPT_URL, $location);
		$return = curl_exec($ch);
		curl_setopt($ch, CURLOPT_URL, 'http://' . $host . '/webmail/addressBookList.do');
		$return = curl_exec($ch);
		curl_close($ch);
		//preg_match_all('/<td width="20%" class="mtb1-td2">\s+.*?target="mainFrame">\s+(.*?)\s+<\/a>\s+<\/td>/', $return, $m1);
		//preg_match_all('/<td class="mtb1-td2">(.*?)<\/td>/', $return, $m2);
		
		preg_match_all('/<td height="34" valign="middle"><a href=\'contactInfo.do\?linkManId=\d+\'>\s+(.*?)\s+<\/a><\/td>\s+<td height="34" valign="middle"><a href=\'contactInfo.do\?linkManId=\d+\'>(.*?)<\/a><\/td>/', $return, $m1);
		//preg_match_all('/<td height="34" valign="middle"><a href=\'contactInfo.do\?linkManId=31\'>(.*?)<\/a><\/td>/', $return, $m2);
		
		if (!count($m1[1])) {
			$this->error = 'err_02';
			//curl_close($ch);
			return false;
		}

		$contacts = array();
		foreach ($m1[1] as $k => $v) {
			$tmp = array();
			$tmp['email'] = iconv('UTF-8', 'GBK', $m1[2][$k]);
			$tmp['name'] = iconv('UTF-8', 'GBK', $v);
			array_push($contacts, $tmp);
		}
		if (!count($contacts)) {
			$this->error = 'err_02';
			return false;
		}
		return $contacts;
	}

	public function get_error()
	{
		return $this->error;
	}
	
	public function clean()
	{
		if(file_exists($this->cookie_jar1))
		{
			@unlink($this->cookie_jar1);
		}
	}
}

?>